export enum PaymentSystem {
  MIR = 'МИР',
  VISA = 'VISA',
  AMEX = 'American Express',
  MASTERCARD = 'MASTERCARD',
  MAESTRO = 'Maestro',
  CHINA_UNIONPAY = 'China UnionPay',
  JCB = 'JCB International',
  UNKNOWN = 'Unknown',
}

const paymentSystemPrefixes: Record<string, PaymentSystem> = {
  '2': PaymentSystem.MIR,
  '4': PaymentSystem.VISA,
  '30': PaymentSystem.AMEX,
  '31': PaymentSystem.AMEX,
  '32': PaymentSystem.JCB,
  '35': PaymentSystem.JCB,
  '36': PaymentSystem.JCB,
  '5': PaymentSystem.MASTERCARD,
  '60': PaymentSystem.MAESTRO,
  '62': PaymentSystem.CHINA_UNIONPAY,
};

export function getPaymentSystem(cardNumber: string): PaymentSystem {
  const prefix = Object.keys(paymentSystemPrefixes).find((prefix) => cardNumber.startsWith(prefix));

  return prefix ? paymentSystemPrefixes[prefix] : PaymentSystem.UNKNOWN;
}
